<?php

namespace App\Http\Controllers\Member;

use App\Models\Category;
use App\Models\CompanyProfile;
use App\Http\Controllers\Core\HomeController as Home;
use App\Models\District;
use App\Models\JobResumeAccess;
use App\Models\Member;
use App\Models\Resume;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class HomeController extends Home
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        if (!$this->isLogin()) {
            return redirect('login');
        }
        if ($this->member->type != 2) {
            return redirect('login');
        }
        // TODO 分类信息后期做缓存
        $category = Category::category_cache();
        //开发地区
        $districts = District::where(['id' => 121])->get();
        $trade = Category::select('id', 'pid', 'name')
            ->where(['alias' => '_trade'])
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
        //获取用户信息
        $member = $this->member;
        //简历信息
        $resume = Resume::where(['user_id' => $member->id])->get()->first();
        //
        if(!$resume){
            return redirect('login');
        }
        $press = Resume::getPress($resume);
        //30日申请职位
        $time = time() - 24 * 60 * 60 * 30;
        $day = JobResumeAccess::where(['user_id' => $member->id])->where('create_time', '>', $time)->get()->count();
        $apply = JobResumeAccess::where(['user_id' => $member->id, 'status' => 3])->get()->count();
        $data = [
            'category' => $category,
            'member' => $member,
            'districts' => $districts,
            'trade' => json_encode($this->list_to_tree($trade)),
            'resume' => $resume,
            'press' => $press,
            'day' => $day,
            'apply' => $apply
        ];
        return view('member.home.index', $data);
    }

    function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
    {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[$data['id']] =& $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][$data['id']] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }

    /*
     * 更新用户基本信息
     * */
    public function ajax_save_basic_info(Request $request)
    {
        $dictionary = Category::category_cache();
        $id = Session::get('member')->id;
        $member = Member::find($id);
        if (!$member->first()) {
            return json_encode(['data' => '', 'msg' => '更新失败', 'status' => 0]);
        }
        $resume = Resume::where(['user_id' => $id])->get();
        if (!$resume = $resume->first()) {
            return json_encode(['data' => '', 'msg' => '更新失败', 'status' => 0]);
        }
        $member->nickname = $request->get('name');
        $member->sex = $request->get('sex');
        $member->birth = $request->get('birth');
        $member->house = $request->get('house');
        $member->education = $request->get('education');
        $member->experience = $request->get('experience');
        if (!$member->save()) {
            return json_encode(['data' => '', 'msg' => '更新失败', 'status' => 2]);
        }
        $resume->nickname = $request->get('name');
        $resume->sex = $request->get('sex');
        $resume->birth = $request->get('birth');
        $resume->house = $request->get('house');
        $resume->education = $request->get('education');
        $resume->education_cn = Category::getCategoryCn($dictionary, '_education', $member->education);
        $resume->experience = $request->get('experience');
        $resume->experience_cn = Category::getCategoryCn($dictionary, '_experience', $member->experience);
        if ($resume->save()) {
            Session::put('member', $member);
            return json_encode(['data' => '', 'msg' => '成功', 'status' => 1]);
        }
        return json_encode(['data' => '', 'msg' => '更新失败', 'status' => 2]);
    }

    /*
     * 更新用户求职意向
     * */
    public function ajax_save_intention(Request $request)
    {
        $user_id = Session::get('member')->id;
        $resume = Resume::where(['user_id' => $user_id])->get();


        if (!$resume = $resume->first()) {
            return json_encode(['data' => '', 'msg' => '更新失败', 'status' => 0]);
        } else {
            $category = Category::category_caches();
            $districts = District::district_cache();
            //求职状态
            $resume->current = $request->get('current');
            $resume->current_cn = Category::getCategoryCn($category, '_current', $resume->current);
            //工作地区
            $resume->district = $request->get('district');
            $resume->district_cn = $districts[$resume->district];
            //期望行业
            $resume->trade = $request->get('trade');
            $resume->trade_cn = Category::getCategoryCns($category, '_trade', $resume->trade);
            //期望职业
            $resume->intention = $request->get('intention');
            $resume->intention_cn = Category::getCategoryCns($category, '_trade', $resume->intention);
            $resume->wage = $request->get('wage');
            $resume->wage_cn = Category::getCategoryCn($category, '_wage', $resume->wage);

            if ($resume->save()) {
                return json_encode(['data' => '', 'msg' => '成功', 'status' => 1]);
            }
            return json_encode(['data' => '', 'msg' => '更新失败', 'status' => 2]);
        }
    }

    /*
     * 更新用户基本详细信息
     * */
    public function ajax_save_specialty(Request $request)
    {

        $user_id = Session::get('member')->id;
        $resume = Resume::where(['user_id' => $user_id])->get();
        if (!$resume = $resume->first()) {
            return json_encode(['data' => '', 'msg' => '更新失败', 'status' => 0]);
        }
        $resume->specialty = $request->get('specialty');
        if ($resume->save()) {
            return json_encode(['data' => '', 'msg' => '成功', 'status' => 1]);
        }
        return json_encode(['data' => '', 'msg' => '更新失败', 'status' => 2]);
    }
}